package com.nit.lab.dao.Impl;

import com.nit.lab.dao.UserDao;
import com.nit.lab.dto.DtoMenu;
import com.nit.lab.dto.DtoUser;
import com.nit.lab.model.SysUserEntity;
import com.nit.lab.utils.SystemConstants;
import com.sun.tools.internal.xjc.model.nav.EagerNClass;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Shaury
 * @date 2017/10/9
 */
@SuppressWarnings("unchecked")
@Repository
public class UserDaoImpl implements UserDao {
    @Resource
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    @Override
    public List<SysUserEntity> getUser4DataTable(Integer start, Integer length, String search) throws Exception {
        List<SysUserEntity> result = null;
        String hql = "from SysUserEntity where isDelete = 0 and concat(userName, userRealName) like '%" + search + "%'order by userId desc";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        result = query.setFirstResult(start).setMaxResults(length).list();
        return result;
    }

    @Override
    public Integer getUser4DataTableCount(String search) throws Exception {
        Integer result = 0;
        String hql = "select count(*) from SysUserEntity where isDelete = 0 and concat(userName, userRealName) like '%" + search + "%'order by userId desc";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        result = Integer.parseInt(query.uniqueResult().toString());
        return result;
    }

    @Override
    public Integer insertUserInfo(SysUserEntity sysUserEntity) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        Session session = this.getSessionFactory().getCurrentSession();
        result = (Integer) session.save(sysUserEntity);
        return result;
    }

    @Override
    public Integer updateUserInfo(SysUserEntity sysUserEntity) throws Exception {
        Integer result = SystemConstants.Update_Default;
        Session session = this.getSessionFactory().getCurrentSession();
        session.update(sysUserEntity);
        result = SystemConstants.Update_Success;
        return result;
    }

    @Override
    public String deleteUserInfo(Integer userId) throws Exception {
        String hql = "update SysUserEntity set isDelete = 1 where userId = " + userId + " ";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        return query.executeUpdate() >= 0 ? "Success" : "Failure";
    }

    @Override
    public boolean isUserHasContent(Integer userId) throws Exception {
        String hql = "select count(*) from SysUserEntity where userId = " + userId + " ";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        return Integer.parseInt(query.uniqueResult().toString()) != 0;
    }

    @Override
    public String getMenuId(Integer actorId) throws Exception {
        String result = null;
        String hql = "select actorMenu from SysActorEntity where actorId =:actorId";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setInteger("actorId", actorId);
        result = query.uniqueResult().toString();
        return result;
    }

    @Override
    public DtoMenu getMenus(Integer menuId) throws Exception {
        DtoMenu result = null;
        String hql = "select new com.nit.lab.dto.DtoMenu( menuId,  menuFather,  menuIcon,  menuUrl,  menuName,  menuType)from SysMenuEntity where menuId =:menuId";
        Session session = this.sessionFactory.getCurrentSession();
        Query query = session.createQuery(hql);
        query.setInteger("menuId", menuId);
        result = (DtoMenu)query.uniqueResult();
        return result;
    }

}
